package top.sunpxit.generator.util;


/**
 * @program: mybatis-plus-generator-puls
 * @description: 字符串工具类
 * @author: sunpx
 * @create: 2020-12-04 15:49
 **/
public class StringUtils {


    /**
     * 下划线转驼峰
     *
     * @param name
     * @return java.lang.String
     * @author: sunpx
     * @date 2020-12-04 15:52:10
     */
    public static String toCamelCase(CharSequence name) {
        if (null == name) {
            return null;
        }

        String name2 = name.toString();
        if (name2.contains("_")) {
            final StringBuilder sb = new StringBuilder(name2.length());
            boolean upperCase = false;
            for (int i = 0; i < name2.length(); i++) {
                char c = name2.charAt(i);

                if (c == '_') {
                    upperCase = true;
                } else if (upperCase) {
                    sb.append(Character.toUpperCase(c));
                    upperCase = false;
                } else {
                    sb.append(Character.toLowerCase(c));
                }
            }
            return sb.toString();
        } else {
            return name2;
        }
    }

    /**
     * 判断字符串是否为空
     *
     * @param str
     * @return boolean
     * @author: sunpx
     * @date 2020-12-04 15:50:56
     */
    public static boolean isEmpty(Object str) {
        return (str == null || "".equals(str));
    }


    /**
     * 首字母大写
     *
     * @param input
     * @return
     */
    public static String upperFirstChar(String input) {
        if (input == null || "".equals(input)) {
            return input;
        }
        StringBuffer buff = new StringBuffer();
        char[] bs = input.toCharArray();
        for (int i = 0; i < bs.length; i++) {
            if (i == 0) {
                buff.append(String.valueOf(bs[0]).toUpperCase());
                continue;
            }
            buff.append(bs[i]);
        }
        return buff.toString();
    }


    public static String toSqlToJava(String sqlType) {
        if (sqlType == null || sqlType.trim().length() == 0) return sqlType;
        sqlType = sqlType.toLowerCase();
        int i = sqlType.indexOf("(");
//        System.out.println(i);
        String substring = sqlType.substring(0, i == -1 ? sqlType.length() : i).toUpperCase();
        switch (substring) {
            case "CHAR":
                return "String";
            case "VARCHAR":
                return "String";
            case "TEXT":
                return "String";
            case "NUMERIC":
                return "BigDecimal";
            case "MONEY":
                return "BigDecimal";
            case "SMALLMONEY":
                return "BigDecimal";
            case "BIT":
                return "boolean";
            case "TINYINT":
                return "int";
            case "SMALLINT":
                return "short";
            case "VARBINARY":
                return "byte[]";
            case "IMAGE":
                return "byte[]";
            case "DATE":
                return "Date";
            case "TIME":
                return "Date";
            case "TIMESTAMP":
                return "Date";
            case "DOUBLE":
                return "Double";
            case "REAL":
                return "Float";
            case "INTEGER":
                return "Integer";
            case "INT":
                return "Integer";
            case "DATETIME":
                return "Date";
            case "BIGINT":
                return "Long";
            default:
                break;
        }

        return substring;
    }
/*
java.math.BigDecimal
java.util.Date
 */
}

